package com.bbx.taxi.client.widget.pop;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.taxi.client.widget.pop.DatePicker.OnConfirmListener;
import com.bbx.taxi.official.R;
import com.example.wheelview.LoopView;
import com.example.wheelview.OnItemSelectedListener;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.PopupWindow.OnDismissListener;

/** 
 * @Path com.bbx.taxi.client.widget.pop.WheelCustom.java
 * @Author 严旭文
 * @Date 2016-1-16 下午4:19:56
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class WheelCustom  {
	private LoopView loop_custom;
	private PopupWindow mPopupWindow_custom;
	private LayoutInflater inflater = null;
	private Context context;
	public static WheelCustom mWheelCustom;
	List<String> list = new ArrayList<String>();

	public static WheelCustom getInstance(Context context) {
		if (mWheelCustom == null) {
			mWheelCustom = new WheelCustom(context);
			return mWheelCustom;

		} else
			return mWheelCustom;
	}

	public WheelCustom(Context context) {
		this.context = context;
		inflater = LayoutInflater.from(context);
	}
	public void onShow(View v,List<String> list,int vtype) {
		if(!mOnWheelCustomStateListenerlist.isEmpty()){
			for(int i=0;i<mOnWheelCustomStateListenerlist.size();i++){
				mOnWheelCustomStateListenerlist.get(i).onWheelCustomState(true);
			}
		}
		initCustomPopView(list,vtype);
		mPopupWindow_custom.showAtLocation(v, Gravity.BOTTOM, 0, 0);
	}
	public boolean isConfirm=false;
	/**
	 * 人数popView
	 */
	public void initCustomPopView(final List<String> list,final int vtype) {
		View popview = getView(list,vtype);
		mPopupWindow_custom = new PopupWindow(popview,
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		mPopupWindow_custom.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow_custom.setContentView(popview);
		mPopupWindow_custom.setOutsideTouchable(false);
		mPopupWindow_custom.setFocusable(true);
		mPopupWindow_custom.setAnimationStyle(R.style.mypopwindow_anim_style);
		mPopupWindow_custom.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				if(!mOnWheelCustomStateListenerlist.isEmpty()){
					for(int i=0;i<mOnWheelCustomStateListenerlist.size();i++){
						mOnWheelCustomStateListenerlist.get(i).onWheelCustomState(false);
					}
					if (mOnWheelCustomSelectedListener != null&&!isConfirm) {
						mOnWheelCustomSelectedListener.onWheelCustomSelected(list,vtype,-1);
					}
					isConfirm=false;
				}
			}
		});
	}

	/**
	 * 获取控件
	 */
	private View getView(final List<String> list,final int vtype) {
		View view = inflater.inflate(R.layout.wheel_custom, null);
		((TextView) view.findViewById(R.id.tv_cancel))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mPopupWindow_custom.dismiss();
						if (mOnWheelCustomSelectedListener != null) {
							mOnWheelCustomSelectedListener.onWheelCustomSelected(list,vtype,-1);
						}
					}
				});
		((TextView) view.findViewById(R.id.tv_confirm))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						isConfirm=true;
						mPopupWindow_custom.dismiss();
						if (mOnWheelCustomSelectedListener != null) {
							mOnWheelCustomSelectedListener.onWheelCustomSelected(list,vtype,loop_custom.getSelectedItem());
						}
					}
				});
		loop_custom = (LoopView) view.findViewById(R.id.loopview);
		loop_custom.setItems(list);
		loop_custom.setListener(new OnItemSelectedListener() {
			
			@Override
			public void onItemSelected(int arg0) {
		}});
		loop_custom.setTextSize(20);
		loop_custom.setNotLoop();
		 //设置初始位置
		loop_custom.setInitPosition(0);
		return view;
	}
	// ///////////////
	/**
	 * 自定义滚轮开关状态监听
	 */
	public static ArrayList<OnWheelCustomStateListener> mOnWheelCustomStateListenerlist = new ArrayList<OnWheelCustomStateListener>();

	/**
	 * 自定义滚轮开关状态监听
	 */
	public interface OnWheelCustomStateListener {
		/**
		 * 自定义滚轮开关状态监听
		 */
		public void onWheelCustomState(boolean isOpen);
	}

	public static void setOnWheelCustomStateListener(
			OnWheelCustomStateListener h) {
		mOnWheelCustomStateListenerlist.add(h);
	}

	public static void removeOnWheelCustomStateListener(
			OnWheelCustomStateListener h) {
		mOnWheelCustomStateListenerlist.remove(h);
	}
	//////////////选中状态///////////////////
	/**
	 * 选中状态
	 */
	public  OnWheelCustomSelectedListener mOnWheelCustomSelectedListener;

	/**
	 * 选中状态开关状态监听,-1则为取消
	 */
	public interface OnWheelCustomSelectedListener {
		public void  onWheelCustomSelected(List<String> list ,int vtype,int position);
	}


	public void setOnSelectedListener(OnWheelCustomSelectedListener l) {
		mOnWheelCustomSelectedListener=l;
	}
}
